package com.buddy.sds.scheduler.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buddy.sds.common.RestApiResponse;
import com.buddy.sds.scheduler.entity.SystemJob;
import com.buddy.sds.scheduler.service.ISystemScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.quartz.UnableToInterruptJobException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/schedule/job")
public class SystemScheduleController {

    @Autowired
    private ISystemScheduleService scheduleService;

    /**
     * 添加任务
     * @param job
     * @return
     */
    @PostMapping("/addJob")
    public RestApiResponse addJob(@RequestBody SystemJob job){
        int status = -1;

        String message = null;

        do{

            if(StringUtils.isEmpty(job.getJobName())){

                message = "任务名称为空";
                break;
            }

            if(job.getJobName().length() > 20){

                message = "任务名称长度在20字符内";
                break;
            }

            if(StringUtils.isEmpty(job.getJobClass())){
                message = "任务类为空";
                break;
            }

            try {
                Class.forName(job.getJobClass());
            } catch (ClassNotFoundException e) {
                //e.printStackTrace();
                message = "任务类未找到";
                break;
            }

            if(StringUtils.isEmpty(job.getJobCorn())){
                message = "corn表达式为空";
                break;
            }

            try {
                if(scheduleService.addJob(job)){
                    status = 0;
                    message = "任务创建成功";
                }else{
                    message = "任务创建失败";
                }
            } catch (SchedulerException e) {
                //e.printStackTrace();
                message = e.getMessage();
            }

        }while (false);

        return new RestApiResponse(status,message,null);
    }

    /**
     * 获取任务列表
     * @param page
     * @return
     */
    @GetMapping("/getJobList")
    public RestApiResponse getJobList(Page page){

        IPage result = scheduleService.page(page);

        return new RestApiResponse(0,"",result);
    }

    /**
     * 停止任务
     * @param jobId
     * @return
     */
    @PutMapping("/stopJob")
    public RestApiResponse stopJob(@RequestParam("jobId") String jobId){

        int status = -1;

        String message = null;

        try {

            if(scheduleService.stopJob(jobId)){

                status = 0;
                message = "任务已停止";
            }else{
                status = -1;
                message = "任务停止失败";
            }
        } catch (Exception e) {
            message = e.getLocalizedMessage();
        }
        return new RestApiResponse(status,message,null);
    }


    /**
     * 暂停任务
     * @param jobId
     * @return
     */
    @PutMapping("/pauseJob")
    public RestApiResponse pauseJob(@RequestParam("jobId") String jobId){

        int status = -1;

        String message = null;

        try {

            if(scheduleService.pauseJob(jobId)){

                status = 0;
                message = "任务已暂停";
            }else{
                status = -1;
                message = "任务暂停失败";
            }
        } catch (SchedulerException e) {
            //e.printStackTrace();

            status = -1;
            message = e.getMessage();
        }
        return new RestApiResponse(status,message,null);
    }

    /**
     * 恢复任务
     * @param jobId
     * @return
     */
    @PutMapping("/resumeJob")
    public RestApiResponse resumeJob(@RequestParam("jobId") String jobId){

        int status = -1;

        String message = null;

        try {

            if(scheduleService.resumeJob(jobId)){

                status = 0;
                message = "任务已恢复";
            }else{
                status = -1;
                message = "任务恢复失败";
            }
        } catch (SchedulerException e) {
            //e.printStackTrace();

            status = -1;
            message = e.getMessage();
        }
        return new RestApiResponse(status,message,null);
    }

    /**
     * 删除任务
     * @param jobId
     * @return
     */
    @DeleteMapping("/deleteJob")
    public RestApiResponse deleteJob(@RequestParam("jobId") String jobId){

        int status = -1;

        String message = null;

        try {

            if(scheduleService.deleteJob(jobId)){

                status = 0;
                message = "任务已删除";
            }else{
                status = -1;
                message = "任务删除失败";
            }
        } catch (SchedulerException e) {
            //e.printStackTrace();

            status = -1;
            message = e.getMessage();
        }
        return new RestApiResponse(status,message,null);
    }

}
